var express = require("express");
//创建路由对象
var router = express.Router();
var users = [
	{'id':'1','userName':'张三','birthday':'2019-02-09','age':'23','password':'123456'},
	{'id':'2','userName':'李四','birthday':'1998-03-19','age':'33','password':'123456'}
];
//首页
router.get("/",function(req,res){
	res.render("index.html",{
		user:req.session.user
	});
});
//登录页面
router.get("/login",function(req,res){
	res.render("login.html");
});
//退出
router.get("/logout",function(req,res){
	req.session.user = null;
	res.redirect("/");
});
//登录页面
router.post("/login",function(req,res){
	var userName = req.body.userName;
	var password = req.body.password;
	var user = users.find(function(item){
		if(item.userName == userName && item.password == password){
			return true;
		}
	});
	if(user){
		req.session.user = user;
		res.status(200).json({
			'code':0,
			'msg':'成功'
		});
	}else{
		res.status(200).json({
			'code':1,
			'msg':'用户名和密码错误'
		});
	}
	
});
//注册页面
router.get("/register",function(req,res){
	res.render("register.html");
});
//注册页面
router.post("/register",function(req,res){
	var user = req.body;
	user.id = Date.now();
	users.push(user);
	res.redirect("/");
});
//导出Router
module.exports = router;